package com.xo.mall.server.impl;

import com.github.pagehelper.PageHelper;
import com.xo.common.Result.CommonPage;
import com.xo.common.Result.ResultResponse;
import com.xo.common.constant.NumberConstant;
import com.xo.common.enums.ResultEnum;
import com.xo.common.pojo.dto.req.BrandCreateReqDto;
import com.xo.common.pojo.dto.req.EditBrandByIdDto;
import com.xo.common.pojo.dto.req.PageBrandListDto;
import com.xo.common.pojo.entity.PmsBrandEntity;
import com.xo.common.pojo.vo.BrandListVo;
import com.xo.common.until.PageUtil;
import com.xo.mall.convert.PmsBrandConvert;
import com.xo.mall.mapper.PmsBrandMapper;
import com.xo.mall.server.PmsBrandService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsBrandServiceImpl implements PmsBrandService {

    @Resource
    private PmsBrandMapper pmsBrandMapper;

    @Override
    public ResultResponse<Object> brandCreate(BrandCreateReqDto brandCreateReqDTO) {
        //校验品牌是否存在 --不能创建相同名称的品牌
        int count = pmsBrandMapper.selectBrandByBrandName(brandCreateReqDTO.getBrandName());
        if (count > 0) {
            return ResultResponse.fail(ResultEnum.THIS_BRAND_EXIST.getMessage());
        }
        PmsBrandEntity pmsBrandEntity = PmsBrandConvert.INSTANCE.BrandCreateReqDTOToPmsBrandEntity(brandCreateReqDTO);
        int num = pmsBrandMapper.brandCreate(pmsBrandEntity);
        if (num > NumberConstant.INT_ZERO) {
            return ResultResponse.success(ResultEnum.CREATE_SUCCESS.getMessage());
        }
        return ResultResponse.fail(ResultEnum.CREATE_FAIL.getMessage());
    }

    @Override
    public ResultResponse<CommonPage<BrandListVo>> brandList(PageBrandListDto pageBrandListDto) {
        PageHelper.startPage(pageBrandListDto.getPageNum(), pageBrandListDto.getPageSize());
        List<BrandListVo> brandListVos = pmsBrandMapper.selectBrandList(pageBrandListDto);
        return ResultResponse.success(PageUtil.getPageList(brandListVos));
    }

    @Override
    public ResultResponse<Object> deleteBrandById(Integer id) {
        int count = pmsBrandMapper.deleteBrandById(id);
        if (count > 0) {
            return ResultResponse.success(ResultEnum.DELETE_SUCCESS.getMessage());
        }
        return ResultResponse.fail(ResultEnum.DELETE_FAIL.getMessage());
    }

    @Override
    public ResultResponse<Object> editBrandById(EditBrandByIdDto editBrandByIdDto) {
        int count = pmsBrandMapper.editBrandById(editBrandByIdDto);
        if (count > 0) {
            return ResultResponse.success(ResultEnum.UPDATE_SUCCESS.getMessage());
        }
        return ResultResponse.fail(ResultEnum.UPDATE_FAIL.getMessage());
    }
}
